package com.x.example.demo;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.x.example.toolbar.ToolbarInitializer;
import com.x.example.toolbar.ToolbarRegistry;
import com.x.example.toolbar.tabpages.HomePage;
import com.x.example.toolbar.tabpages.MessagePage;
import com.x.example.toolbar.tabpages.UserCenterPage;
import com.x.example.toolbar.tabpages.DiscoverTabPage;
import com.example.uiframework.utils.Utils;
import com.x.uikit.toolbar.XTabToolbar;
import com.x.uikit.toolbar.XToolbar;
import com.x.uikit.toolbar.XToolbarItem;

import java.util.ArrayList;
import java.util.List;

public class BottomTabDemo extends com.x.uikit.page.XPage {
    private ViewPager2 mPages;
    private XTabToolbar mToolbar;
    private ViewModel mViewModel = new ViewModel();

    public BottomTabDemo(@NonNull Context context) {
        super(context);
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        this.addView(layout);
        mPages = new ViewPager2(getContext());
        LinearLayout.LayoutParams lpPages = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        lpPages.weight = 1;
        layout.addView(mPages, lpPages);
        mPages.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
//                mToolbar.updateSelectItemProgress(mViewModel.getToken(position),positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mToolbar.selectItem(mViewModel.getToken(position));
            }
        });
        mPages.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                String token = mViewModel.getToken(viewType);
                View page = mViewModel.getPage(parent.getContext(), token);
                if (page == null) {
                    page = new View(page.getContext());
                }
                ViewGroup.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                page.setLayoutParams(lp);
                return new RecyclerView.ViewHolder(page) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
            }

            @Override
            public int getItemViewType(int position) {
                return position;
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

            }

            @Override
            public int getItemCount() {
                return mViewModel.getCount();
            }
        });
        View divider = new View(getContext());
        divider.setBackgroundColor(0xff999999);
        LinearLayout.LayoutParams lpDivider = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.res.dpI(0.5f));
        layout.addView(divider, lpDivider);

        mToolbar = new XTabToolbar(getContext());
        mToolbar.setItemCreator(ToolbarRegistry.get());
        mToolbar.setItemLayoutParamsGenerator(new XToolbar.IItemLayoutParamsGenerator() {
            @NonNull
            @Override
            public LinearLayout.LayoutParams generateLayoutParams(String token) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.CENTER_VERTICAL;
                layoutParams.weight = 1;
                return layoutParams;
            }
        });
        LinearLayout.LayoutParams lpToolbar = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.res.dpI(50));
        layout.addView(mToolbar, lpToolbar);
        mToolbar.setItems(mViewModel.getTokens());
        mToolbar.selectItem(ToolbarInitializer.TOKEN_HOME, false);
        mToolbar.setItemSelectAnimateDuration(1500);
        mToolbar.setItemClickListener(new XToolbar.OnItemClickListener() {
            @Override
            public void onItemClicked(String token, XToolbarItem item) {
                int position = mViewModel.getPosition(token);
                if (position < 0) {
                    position = 0;
                }
                mPages.setCurrentItem(position, true);

            }
        });

    }


    private static class ViewModel {
        private List<String> mTokens = new ArrayList<>();


        public ViewModel() {
            mTokens.add(ToolbarInitializer.TOKEN_HOME);
            mTokens.add(ToolbarInitializer.TOKEN_X);
            mTokens.add(ToolbarInitializer.TOKEN_MESSAGE);
            mTokens.add(ToolbarInitializer.TOKEN_USER_CENTER);
        }

        public View getPage(Context context, String token) {
            if (TextUtils.equals(token, ToolbarInitializer.TOKEN_HOME)) {
                return new HomePage(context);
            } else if (TextUtils.equals(token, ToolbarInitializer.TOKEN_X)) {
                return new DiscoverTabPage(context);
            } else if (TextUtils.equals(token, ToolbarInitializer.TOKEN_MESSAGE)) {
                return new MessagePage(context);
            } else if (TextUtils.equals(token, ToolbarInitializer.TOKEN_USER_CENTER)) {
                return new UserCenterPage(context);
            }
            return null;
        }

        public int getCount() {
            return mTokens.size();
        }

        public String getToken(int position) {
            if (position > mTokens.size() - 1 || position < 0) {
                return null;
            }
            return mTokens.get(position);
        }

        public int getPosition(String token) {
            return mTokens.indexOf(token);
        }

        public List<String> getTokens() {
            return mTokens;
        }
    }
}
